package com.yishusmt.wechatapi.wechatdev.serviceImpl;

import com.yishusmt.wechatapi.wechatdev.constant.OrderConstant;
import com.yishusmt.wechatapi.wechatdev.dto.base.Result;
import com.yishusmt.wechatapi.wechatdev.dto.response.PictureRes;
import com.yishusmt.wechatapi.wechatdev.dto.response.SupplierRes;
import com.yishusmt.wechatapi.wechatdev.entity.SupplierCountEntity;
import com.yishusmt.wechatapi.wechatdev.entity.SupplierEntity;
import com.yishusmt.wechatapi.wechatdev.mapper.SupplierEntityMapper;
import com.yishusmt.wechatapi.wechatdev.mapper.SupplierInfoMapper;
import com.yishusmt.wechatapi.wechatdev.mapper.SupplierPictureInfoMapper;
import com.yishusmt.wechatapi.wechatdev.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zhangyi
 * @date 2018/9/7 14:58
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierPictureInfoMapper supplierPictureInfoMapper;

    @Autowired
    private SupplierEntityMapper supplierEntityMapper;

    /**
     * 加工厂列表
     *
     * @return
     */
    @Override
    public Result getSupplierList(Integer count,Integer status) {
        SupplierCountEntity supplierCountEntity = new SupplierCountEntity();
        supplierCountEntity.setCount(count);
            supplierCountEntity.setStatus(OrderConstant.STATUS.equals(status)?null:status);

        List<SupplierRes> supplierRes =
                supplierPictureInfoMapper.supplierList(supplierCountEntity);
        PictureRes pictureResTemp = new PictureRes();
        //获取封面图片
        supplierRes.stream().forEach(e -> {
            List<PictureRes> pictureRes = supplierPictureInfoMapper.selectPicture(e.getSupplierId());
            if(pictureRes.size() > 1) {
                pictureRes.stream().forEach(t -> pictureResTemp.setImageName(t.getImageName()));
                Optional<PictureRes> first = pictureRes.stream().findFirst();
                PictureRes p = first.orElse(pictureResTemp);
                e.setImgName(p.getImageName());
            }
        });
        return new Result(supplierRes);
    }

    /**
     * 加工厂详细信息
     *
     * @param id
     * @return
     */
    @Override
    public Result getSupplierInfo(Long id) {
        SupplierEntity supplierEntity =
                supplierEntityMapper.selectByPrimaryKey(id);
        List<PictureRes> pictureRes = supplierPictureInfoMapper.selectPicture(id);
        List<String> imageName = new ArrayList<>();
        pictureRes.stream()
                .limit(4)
                .forEach(e -> imageName.add(e!=null?e.getImageName():null));
        supplierEntity.setImages(imageName);
        return new Result(supplierEntity);
    }
}
